#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 310;

int p[N*10][N],f[N*10][N],x[N*10],w[N*10][N*10],n,m;

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;m = in; for(int i = 1;i <= n;i++) x[i] = in;
	sort(x + 1,x + n + 1); for(int l = 1;l <= n;l++) {w[l][l] = 0;for(int r = l+1;r <= n;r++) w[l][r] = w[l][r-1] + x[r] - x[(l + r) / 2];}
	memset(f,0x3f,sizeof(f)); f[0][0] = 0;
	for(int i = 1;i <= m;i++){
		p[n + 1][i] = n;
		for(int j = n;j >= 1;j--){
			int ps = 0;
			for(int k = p[j][i-1];k <= p[j+1][i];k++)
				if(f[k][i-1] + w[k+1][j] < f[j][i]) f[j][i] = f[k][i-1] + w[k+1][j],ps = k;
			p[j][i] = ps; //cerr<<ps<<endl;
		}
	}
	printf("%d\n",f[n][m]);
	return 0;
}

